package net.hzlj.platform.enterprise.helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.rl.zhangjiajie.R;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

/**
 * @ClassName: ImageLoaderHelper
 * @Description: 图片缓存
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日 下午10:17:26
 */
public class ImageLoaderHelper {
	
	public static final int DISK_CACHE_SIZE = 100 * 1024 * 1024;// 100 Mb
	
	public static void initImageLoader(){
		if (!ImageLoader.getInstance().isInited()) {
			ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(BootApp.getAppContext())
					.threadPriority(Thread.MIN_PRIORITY)
					.denyCacheImageMultipleSizesInMemory()
					.diskCacheFileNameGenerator(new Md5FileNameGenerator())
					.diskCacheSize(20 * 1024 * 1024)
					.memoryCacheSizePercentage(20)
					// 缓存到文件的最大数据
					.tasksProcessingOrder(QueueProcessingType.LIFO)
					.writeDebugLogs() // Remove for release app
					.threadPoolSize(5)
					.build();
			ImageLoader.getInstance().init(config);
		}
	}

	public static DisplayImageOptions getDisplayImageOptions() {
		initImageLoader();
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inSampleSize = 2;
		return new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.ic_default_head)
				.showImageForEmptyUri(R.drawable.ic_default_head)
				.showImageOnFail(R.drawable.ic_default_head)
				.resetViewBeforeLoading(true).cacheOnDisk(true)
				.decodingOptions(options)
				.imageScaleType(ImageScaleType.EXACTLY)
				.bitmapConfig(Bitmap.Config.ARGB_4444).considerExifParams(true)
				.displayer(new FadeInBitmapDisplayer(300)).build();
	}

}
